/**
 *	TrueMusic - simple java mp3 player
 *
 *	TrueMusic is free software; you can redistribute it
 *	and/or modify it under the terms of the GNU General
 *	Public License as published by the Free Software
 *	Foundation; either version 2 of the License or (at
 *	your option) any later version.
 *	
 *	This program is distributed in the hope that it will be
 *	useful but WITHOUT ANY WARRANTY; without even the 
 *	implied warranty of the MERCHANTABILITY or FITNESS FOR
 *	A PARTICULAR PURPOSE. See the GNU General Public
 *	License for more details.
 *	
 *	Copyright (c)2005-2006 Ryan Macnak <ambiguousprep@gmail.com>
 */


package com.servequake.truemusic;

import java.io.*;

// abstraction for multiple format support
// gui is above/audio playback is below

public abstract class AudioFile extends Thread
{
	protected AudioFile()
	{
		setPriority(MAX_PRIORITY);
	}

	public abstract void play();
	public abstract void pause();
	public abstract void setPos(int npos);
	public abstract void stop2();

	public abstract String getTitle();
	public abstract String getArtist();
	public abstract String getAlbum();
	public abstract int getFrames();

	public abstract String filename();


	// nullify any resources being consumed by this object
	public abstract void dispose();

	static AudioFile open(java.io.File f, javax.swing.JSlider prog) throws Exception
	{
		String x = f.toString().toLowerCase();

		if( x.endsWith(".mp3") )
		{
			try
			{
				return new MP3File(f.toString(),prog);
			}
			catch(Exception e)
			{
				return new GenericAudioFile(f,prog);
			}
		}
		else if( x.endsWith(".wav") )   
		{
			return new GenericAudioFile(f,prog);
		}
	//	else if( x.endsWith(".cda") )   
	//	{
	//		return new GenericAudioFile(f,prog);
	//	}
		else if( x.endsWith(".aif") )
		{
			return new AppletClipAudioFile(f);
		}
		else if( x.endsWith(".au") )
		{
			return new AppletClipAudioFile(f);
		}
		else if( x.endsWith(".m3u") )
		{
				InputStream is = new FileInputStream(f);
				BufferedReader in = new BufferedReader(new InputStreamReader(is));

				while( true )
				{
					String y = in.readLine();
					if(y==null)break;

					try
					{
						TrueMusic.inst.pp.addFile( new File(y) );
					}
					catch(Exception ex)
					{}
				}
	
				in.close();
				is.close();

			return null;
		}
		else
		{
			return null;
		}
	}
}
